﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using WawaSoft.WawaCapturer;

namespace SharpSniffer
{
    /// <summary>
    /// 特殊的抓包--南车OTA
    /// </summary>
    public class CaptureOTA : Capture
    {

        /// <summary>
        /// 日志写入
        /// </summary>
        protected WriteLog2 writeLog = new WriteLog2();
        public override void OperData(byte[] buf, int recvedSize, long index)
        {
            //取池
            PacketArrivedEventArgs pakobj = pool.Count > 0 ? pool.Dequeue() : new PacketArrivedEventArgs();
            //处理数据
            var d = Receive(buf, recvedSize);

            //处理IP头
            IPHead iph = IPHead.Parse(buf, ipPack);

            if ((d.Protocol == "TCP"// || d.Protocol == "UDP"
                )
                //判断 端口过滤
                && (d.OriginationPort != 3389&&d.DestinationPort!=3389)
                && (d.OriginationPort != 1521 && d.DestinationPort != 1521)
                //&& (d.DestinationPort == 60017 || d.OriginationPort == 60017)
                && d.MessageBuffer.Length > 20
                )
            {
                //Print(ConsoleSperatorHf);
                //Print(index);
                //PrintLine(ConsoleSperatorHf);
                //Console.WriteLine(d);

                #region UDP
                if (ipPack.protocol == ProtocolType.Udp)
                {
                    UDPPackeg packUdp = UDPPackeg.Parse(buf, ipPack.ihl * 4, udpPack);


                    //过滤
                    int datalen = d.MessageBuffer.Length - 8;
                    if (datalen > 0)
                    {
                        byte[] data = new byte[datalen];
                        Buffer.BlockCopy(d.MessageBuffer, 8, data, 0, datalen);

                       



                        //200 毫秒打印一次
                        if (Printdt < DateTime.Now)
                        {

                            Console.WriteLine(string.Format("Pack{0}收到数据:{1};{2}", d.Protocol,
        System.Text.Encoding.GetEncoding("GB2312").GetString(data, 0, data.Length > 30 ? 30 : data.Length)
        , BitConverter.ToString(data, 0, data.Length > 30 ? 30 : data.Length).Replace("-", string.Empty)));

                            Printdt = DateTime.Now.AddMilliseconds(200);
                        }


                    }


                }
                #endregion


               // if (ipPack.protocol == ProtocolType.Tcp)
                if (d.Protocol =="TCP")
                {
                    TCPPackeg packTcp = TCPPackeg.Parse(buf, ipPack.ihl * 4, recvedSize, tcpPack);
                    if (packTcp.TCPData.Length > 0)
                    {
                        //var den = d.MessageBuffer.Length - 20 == packTcp.TCPData.Length;
                        //if (den)
                        //{
                   
                            //写日志
                            writeLog.Write(string.Format("ori{0}_{1}des{2}_{3}",d.OriginationAddress,d.OriginationPort, d.DestinationAddress, d.DestinationPort), BitConverter.ToString(packTcp.TCPData).Replace("-",string.Empty));
                        //}
                    }


                }


            }
        }
    }
}
